<template>
  <div id="spread-host">
    <gc-spread-sheets
      hostClass="spreadHost"
      @workbookInitialized="initWorkbook"
    >
    </gc-spread-sheets>
  </div>
</template>

<script>
import "@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2016colorful.css";
import * as GC from "@grapecity-software/spread-sheets";
import "@grapecity-software/spread-sheets-vue";
import { Utility, SpreadCommon } from "@/libs/utility";

let originUrl = window.location.origin;
let licenseKey = "";
if (originUrl == "http://192.168.13.13") {
  licenseKey =
    "ZNY,yqgzs,713E0CB2EDB4491#B1XaYxUWwQjbwMlSxRWeDhmRLxmRlFzawlmR8pnSkJiOiMlIs4XX3JyUXJDNiojIDJCLicTMuYHIu3GZkFULyVmbnl6clRULTpEZhVmcwNlI0IiTisHL3JSVBtUUiojIDJCLicTMuYHITpEIkFWZyB7UiojIOJyebpjIkJHUiwiIxMjMxkTO9IjI0ICc8VkIsU6csFmZ0ICb6VkIsICdz3GasF6YvxGLzEjLzEjL8YTMuITOxIiOiMXbEJCL35lI4VWZoNFd49WYHJCLiQXZlh6U4J7bwVmUiwiIlxmYhRFdvZXaQJyW0IyZsZmIsU6csFmZ0IiczRmI1pjIs9WQiwiIxADMwITMgEDMxAzMyAjMiojI4J7QiwiIt36YuEXcAlTN8YTO7IDOiojIh94QiwiIxkDN4IERFJjQDBTRzEzNiojIklkI1pjIEJye3JSODZkMycjQ4IiOigkIsISP3cWOrhkVPZlZ9JnQvlFT74UUtRUMUFmW5tEMM3GRtF5Qzo5MulkbXhFSHtUStVHWphTW0VkQsljYCZEMhN5bLlneEVFRhJUNyt6QiRUdww4VZNjYwAlbpdWNDtURY36NH9kb8E5Z7kjUDd7MEdlb5YmdXl4dEFDdo5EM8gjSpdmUXlWNoJ5MFJHenhWcUlmcxgGVVBlaygFaWR5NrJVQ7YDVjVnQUd6R5h4UDxmcmtETFRUWaJWN4kXRaVDUwR5VwNEa9U5R0BDe7g5cxMnRrsmZmhkb7lmdwkUcJJVV6E7dVdHVuhUUSZVQaVHOyo7T6V6c4oEOkJzR0l7M4tkc5MUbqFXTp36V7k6U62kRQFkVrN6MJFFNrMjbwd5YwQlcYdUTsJES59mWFZ6aRFza6JGSNNHb8sSM3t";
} else if (
  originUrl == "https://bmbimfamilylib.cpolar.top" ||
  originUrl == "https://zk.dayinjiaoyu.cn"
) {
  licenseKey =
    "ZNY,yqgzs,713E0CB2EDB4491#B1lRMxmRlFzawlmR8pnSkJiOiMlIs4XX3JyUXJDNiojIDJCLicTMuYHIu3GZkFULyVmbnl6clRULTpEZhVmcwNlI0IiTisHL3JSVBtUUiojIDJCLicTMuYHITpEIkFWZyB7UiojIOJyebpjIkJHUiwiIxMjMxkTO9IjI0ICc8VkIsU6csFmZ0ICb6VkIsICdz3GasF6YvxGLuNmL5l7bhlmaulWehRmLrpHLw3GduIXYs3Gcj9iYpxWeslWbhZWbpJWbiJiOiMXbEJCL35lI4VWZoNFd49WYHJCLiQXZlh6U4J7bwVmUiwiIlxmYhRFdvZXaQJyW0IyZsZmIsU6csFmZ0IiczRmI1pjIs9WQiwiIxADMwITMgEDMxAzMyAjMiojI4J7QiwiIt36YuEXcAlTN8YTO7IDOiojIh94QiwiIxkDN4IERFJjQDBTRzEzNiojIklkI1pjIEJye#4nIyADN8kDMFJiOigkIsISP3cWOrhkVPZlZ9JnQvlFT74UUtRUMUFmW5tEMM3GRtF5Qzo5MulkbXhFSHtUStVHWphTW0VkQsljYCZEMhN5bLlneEVFRhJUNyt6QiRUdww4VZNjYwAlbpdWNDtURY36NH9kb8E5Z7kjUDd7MEdlb5YmdXl4dEFDdo5EM8gjSpdmUXlWNoJ5MFJHenhWcUlmcxgGVVBlaygFaWR5NrJVQ7YDVjVnQUd6R5h4UDxmcmtETFRUWaJWN4kXRaVDUwR5VwNEa9U5R0BDe7g5cxMnRrsmZmhkb7lmdwkUcJJVV6E7dVdHVuhUUSZVQaVHOyo7T6V6c4oEOkJzR0l7M4tkc5MUbqFXTp36V7k6U62kRQFkVrN6MJFFNrMjbwd5YwQlcYdUTsJES59mWFZ6aRFza6JGSNNHb8s6MstyaxxUWwQjbwMlSxRdewh";
} else if (originUrl.indexOf("localhost") != -1) {
  licenseKey =
    "ZNY,yqgzs,713E0CB2EDB4491#B1JQWbwMlSxRWeDhmRLxmRlFzawlmR8pnSkJiOiMlIs4XX3JyUXJDNiojIDJCLigTMuYHIu3GZkFULyVmbnl6clRULTpEZhVmcwNlI0IiTisHL3JCSJpkQiojIDJCLigTMuYHITpEIkFWZyB7UiojIOJyebpjIkJHUiwiIxMjMxkTO9IjI0ICc8VkIsU6csFmZ0ICb6VkIsICdz3GasF6YvxGLwADMwMjO4N7boxWYj3Gbv2iOwRHdoJiOiMXbEJCL35lI4VWZoNFd49WYHJCLiQXZlh6U4J7bwVmUiwiIlxmYhRFdvZXaQJyW0IyZsZmIsU6csFmZ0IiczRmI1pjIs9WQiwiIxADMwITMgEDMxAzMyAjMiojI4J7QiwiIt36YuEXcAlTN8YTO7IDOiojIh94QiwiIxkDN4IERFJjQDBTRzEzNiojIklkI1pjIEJye3JCN8ADOzMDM4IiOigkIsISP3cWOrhkVPZlZ9JnQvlFT74UUtRUMUFmW5tEMM3GRtF5Qzo5MulkbXhFSHtUStVHWphTW0VkQsljYCZEMhN5bLlneEVFRhJUNyt6QiRUdww4VZNjYwAlbpdWNDtURY36NH9kb8E5Z7kjUDd7MEdlb5YmdXl4dEFDdo5EM8gjSpdmUXlWNoJ5MFJHenhWcUlmcxgGVVBlaygFaWR5NrJVQ7YDVjVnQUd6R5h4UDxmcmtETFRUWaJWN4kXRaVDUwR5VwNEa9U5R0BDe7g5cxMnRrsmZmhkb7lmdwkUcJJVV6E7dVdHVuhUUSZVQaVHOyo7T6V6c4oEOkJzR0l7M4tkc5MUbqFXTp36V7k6U62kRQFkVrN6MJFFNrMjbwd5YwQlcYdUTsJES59mWFZ6aRFza6JGSNNHb8s6MstyauxXW";
}
var self = null;
GC.Spread.Sheets.LicenseKey = licenseKey;

export default {
  props: {
    /** 数据源：这里不确定字段，只要是数组就可以 */
    dataSource: {
      /** 这里不确定字段，只要是数组就可以 */
      type: Array,
      required: true,
    },
  },
  components: {},
  data() {
    return {};
  },
  methods: {
    initWorkbook: (spread) => {
      var str = sessionStorage.getItem("globalData");
      if (str) {
        str = JSON.parse(str);
      }
      var newData = Utility.arrayFlat(str, (t) => t.children);
      console.log(newData, "newData....");
      spread.options.tabStripVisible = false; // 把sheet隐藏
      spread.options.allowContextMenu = false;
      spread.options.newTabVisible = false;
      spread.options.allowUserDragDrop = false; // 不允许移动，不会出现移动的鼠标样式
      spread.options.allowUserDragFill = false; //  不允许拉动填满，不会出现十字架的鼠标样式
      spread.options.allowUserResize = true;
      spread.options.scrollbarMaxAlign = true;
      spread.options.scrollbarShowMax = true;

      let sheet = spread.getActiveSheet();
      sheet.reset();
      sheet.rowFilter(
        new GC.Spread.Sheets.Filter.HideRowFilter(
          new GC.Spread.Sheets.Range(-1, -1, -1, -1)
        )
      );

      var filter = sheet.rowFilter();
      filter.filterDialogVisibleInfo({
        sortByValue: false,
        sortByColor: false,
        filterByColor: true,
        filterByValue: true,
        listFilterArea: true,
      });

      sheet.suspendPaint();
      sheet.options.isProtected = true;
      sheet.options.protectionOptions = {
        // 允许折叠行
        allowOutlineRows: true,
        // 布尔值，用户是否可以对一片单元格区域进行排序。
        allowSort: true,
        // 布尔值，用户是否可以对一片单元格区域进行筛选。
        allowFilter: true,
        // 布尔值，用户是否可以改变列宽。
        allowResizeColumns: true,
      };
      var initColumn = [
        { name: "FolderName", displayName: "目录名称", size: 300 },
        { name: "EnCode", displayName: "编码", size: 300 },
        { name: "FolderTypeName", displayName: "目录类型", size: 200 },
      ];

      sheet.setDataSource(newData);
      sheet.bindColumns(initColumn);
      // let parentColumnIndex: number[] = []
      for (let r = 0, len = newData.length; r < len; r++) {
        let level = newData[r].level ? newData[r].level : 1;
        // if (level == 1) {
        //     parentColumnIndex.push(r)
        // }
        sheet.getCell(r, 0).textIndent(level);
        // // 可编辑
        // if (datasource[r].hasChildren == false && datasource[r].relatedCount < 1 && formulaData.filter((b: any) => b.itemId == datasource[r].id).length < 1) {
        //     SpreadCommon.setEditColorForCustom(sheet, r, 1)
        // }
      }

      SpreadCommon.initOutlineColumn(sheet, false);
      // 冻结列
      sheet.frozenColumnCount(1);
      //冻结线颜色设置
      sheet.options.frozenlineColor = "#e6e6e6";
      SpreadCommon.setSheetHeight(sheet);
      SpreadCommon.setCommonColor(sheet);
      // 先解除所有绑定，不然会绑定很多次
      sheet.unbindAll();

      /** 单元格点击事件 */
      sheet.bind(GC.Spread.Sheets.Events.CellClick, function (sender, args) {
        if (args.sheetArea === GC.Spread.Sheets.SheetArea.colHeader) {
          return;
        }
        let tempRows = [];
        let tempCols = [];
        let tempData = [];
        var selections = sheet.getSelections();
        if (selections && selections.length > 0) {
          selections.forEach((item) => {
            for (
              let index = item.row;
              index < item.row + item.rowCount;
              index++
            ) {
              if (
                (tempData.length == 0 ||
                  tempData?.filter((t) => t.Id == newData?.[index]?.Id)
                    ?.length < 1) &&
                newData?.[index]?.Id
              ) {
                tempData.push(newData?.[index]);
                tempRows.push(index);
              }
            }
            for (
              let colIndex = item.col;
              colIndex < item.col + item.colCount;
              colIndex++
            ) {
              tempCols.push(colIndex);
            }
          });
        }
        console.log(tempRows, self, "tempRows.......");
        self.$emit("cellClick", {
          tempData,
          tempCols,
          tempRows,
        });
        // props?.onRowSelect && props?.onRowSelect(tempData, tempCols, tempRows);
      });

      sheet.resumePaint();
    },
  },
  mounted() {
    self = this;
  },
};
</script>

<style scoped>
.spreadHost {
  width: 100%;
  height: calc(100vh - 150px);
}
</style>